package monitor;

import java.io.IOException;

import maquina.LocalProperties;
import operacoes.GridMonitorOperation;
import operacoes.Mensagem;
import servidor.FileServerSocket;
import servidor.ServerHamsterSocket;
import cliente.ClientSocket;
import files.GridMonitorFileManager;
import files.ComponentsListFileManager;
import gui.TelaGeral;

public class MainMonitor {

	public static final int interno = 1;
	public static final int externo = 2;
	
	public static void main(String args[]){
		try{
			
			LocalProperties.start();
			
			ServerHamsterSocket ssInterno = new ServerHamsterSocket(LocalProperties.portaInterna, MainMonitor.interno);
			ServerHamsterSocket ssExterno = new ServerHamsterSocket(LocalProperties.portaExterna, MainMonitor.externo);
			FileServerSocket fileServerSocket = new FileServerSocket();
			Heartbeat monitorHeartBeat = new Heartbeat();
			HeartbeatLocal heartBeatLocal = new HeartbeatLocal();
			
			ssInterno.start();
			ssExterno.start();
			fileServerSocket.start();
			monitorHeartBeat.start();
			heartBeatLocal.start();

			//Verify if the components monitored before the failure, already work.
			if(!GridMonitorFileManager.isEmptyList(LocalProperties.filenameComponentsList)){
				 //Retrieve the list of components
				 String componentsList[] = GridMonitorFileManager.retrieveInformation(LocalProperties.filenameComponentsList).split(LocalProperties.separador); 
				 
	    		 int index = 1;
	    		 int cont = 0;
	    		 int quantidadeUsuarios = Integer.parseInt(componentsList[0]);
	    		 
	    		 while(cont < quantidadeUsuarios){
	    			int tipoComponente = Integer.parseInt(componentsList[index]); 
	    			int portaComunicacao = Integer.parseInt(componentsList[index+1]);
	    			
	    			try{
	    			   ClientSocket.enviarMSG("127.0.0.1", portaComunicacao, Mensagem.montarMensagem(TipoOperacao.GRID_MONITOR_RECONNECT+""));
	    			}catch(IOException io){ //If the component isn't connected, remove it from the list of components
	    			   ComponentsListFileManager.removeComponentsFromList(tipoComponente, portaComunicacao);
	    			   System.out.println(io.getMessage());
	    			}
	                index=index+2;
	                cont++;
	    		 }
			}
			
			//Verify if the nodes monitored before the failure, already work.
			if(!GridMonitorFileManager.isEmptyList(LocalProperties.filenameNodesList)){
				 //Retrieve the list of components
				 String nodesList[] = GridMonitorFileManager.retrieveInformation(LocalProperties.filenameNodesList).split(LocalProperties.separador); 
				 
	    		 int index = 1;
	    		 int cont = 0;
	    		 int quantidadeMonitores = Integer.parseInt(nodesList[0]);
	    		 String nodeIP = null;
	    		 while(cont < quantidadeMonitores){
	    			nodeIP = nodesList[index]; 
	    			
	    			GridMonitorOperation.join(nodeIP);
	                index=index+2;
	                cont++;
	    		 }
			}
			
			TelaGeral tg = new TelaGeral();
			tg.start();
			System.out.println("Hamster ativo.");
		} catch (IOException e){
			System.out.println(e.getMessage());
		} catch (Exception e){
			System.out.println(e.getMessage());
		}
	}
}	
